package com.lu.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.article.mapper.ApArticleConfigMapper;
import com.lu.article.mapper.ApArticleContentMapper;
import com.lu.article.mapper.ApArticleIndexMapper;
import com.lu.article.service.ApArticleFreemarkerService;
import com.lu.article.service.ApArticleIndexService;
import com.lu.common.constants.ArticleContants;
import com.lu.model.article.dto.ArticleDto;
import com.lu.model.article.dto.ArticleIndexDto;
import com.lu.model.article.pojo.ApArticle;
import com.lu.model.article.pojo.ApArticleConfig;
import com.lu.model.article.pojo.ApArticleContent;
import com.lu.model.common.dtos.ResponseResult;
import com.lu.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ApArticleIndexServiceImpl extends ServiceImpl<ApArticleIndexMapper, ApArticle> implements ApArticleIndexService {

    //每页最多加载条数
    private static final short MAX_PAGE_SIZE = 50;

    @Autowired
    private ApArticleIndexMapper articleIndexMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleFreemarkerService apArticleFreemarkerService;

    /**
     *
     * @param dto 条件对象
     * @param loadtype 标签，查询方式
     * @return
     */
    @Override
    public ResponseResult loadIndexArticle(ArticleIndexDto dto, Short loadtype) {
        //对参数进行校验
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            size = 10;
        }
        size = Math.min(size, MAX_PAGE_SIZE);

        //判断加载方式,更多，最新，还是默认，已经频道
        //默认
        if (!loadtype.equals(ArticleContants.LOADTYPE_LOAD_MORE) && !loadtype.equals(ArticleContants.LOADTYPE_LOAD_NEW)) {
            loadtype = ArticleContants.LOADTYPE_LOAD_MORE;

        }
        //频道
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleContants.LOADTYPE_TAG_DEFAULT);
        }

        //时间
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }

        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        //查询
        List<ApArticle> apArticles = articleIndexMapper.selectIndexArticle(dto, loadtype);

        return ResponseResult.okResult(apArticles);
    }


    /**
     * 保存app端文章
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        //如果id不存在就保存，存在就是修改
        //参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //数据库是否存在
        ApArticle article = this.getOne(Wrappers.<ApArticle>lambdaQuery().eq(ApArticle::getId, dto.getId()));

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        //保存还是修改
        if (article == null) {
            //保存
            //文章
            this.save(apArticle);
            //配置
            ApArticleConfig config = new ApArticleConfig(dto.getId());
            apArticleConfigMapper.insert(config);
            //内容
            ApArticleContent content = new ApArticleContent();
            content.setArticleId(dto.getId());
            content.setContent(dto.getContent());
            apArticleContentMapper.insert(content);
        } else {
            //修改
            //文章
            this.updateById(apArticle);
            //内容
            ApArticleContent apArticleContent = apArticleContentMapper
                                                .selectOne(Wrappers
                                                .<ApArticleContent>lambdaQuery()
                                                .eq(ApArticleContent::getArticleId, dto.getId()));
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.updateById(apArticleContent);
        }

        //异步调用
        apArticleFreemarkerService.buildArticleToMinIO(apArticle, dto.getContent());

        return ResponseResult.okResult(apArticle.getId());
    }
}
